package com.mall.service.authority.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.*;
import com.mall.model.vo.PageResult;
import com.mall.service.authority.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;

    @Autowired
    private UmsMenuService umsMenuService;

    @Autowired
    private UmsRoleMenuRelationService umsRoleMenuRelationService;

    @Autowired
    private UmsResourceService umsResourceService;

    @Autowired
    private UmsRoleResourceRelationService umsRoleResourceRelationService;




    /**
     * 获取所有角色
     *
     * @return
     */
    @GetMapping("/listAll")
    public CommonResult listAll() {
        List<UmsRole> list = umsRoleService.list();
        return CommonResult.success(list);
    }

    /**
     * 根据角色名称分页获取角色列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize, String keyword) {

        PageResult pr = umsRoleService.getList(pageNum, pageSize, keyword);

        return CommonResult.success(pr);
    }

    /**
     * 添加角色
     *
     * @param umsRole
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody UmsRole umsRole) {
        umsRoleService.save(umsRole);
        return CommonResult.success("添加成功");
    }

    /**
     * 删除角色
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam List<Integer> ids) {
        umsRoleService.removeByIds(ids);
        //删除角色对应的菜单信息
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UmsRoleMenuRelation::getRoleId,ids);
        umsRoleMenuRelationService.remove(queryWrapper);
        return CommonResult.success("删除成功");
    }

    /**
     * 修改角色
     *
     * @param id
     * @param umsRole
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Integer id, @RequestBody UmsRole umsRole) {
        umsRoleService.updateById(umsRole);
        return CommonResult.success("修改成功");
    }

    /**
     * 修改账号状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable("id") Integer id, Integer status) {
        UmsRole role = umsRoleService.getById(id);
        role.setStatus(status);
        umsRoleService.updateById(role);
        return CommonResult.success(null);
    }

    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    public CommonResult listMenu(@PathVariable Integer roleId){

        List<UmsMenu> list = umsMenuService.listMenu(roleId);

        return CommonResult.success(list);
    }

    /**
     * 给角色分配菜单
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("/allocMenu")
    public CommonResult allocMenu(Integer roleId,@RequestParam List<Integer> menuIds){
        umsRoleMenuRelationService.allocMenu(roleId,menuIds);
        return CommonResult.success(null);
    }


    /**
     * 获取角色相关资源
     * @param roleId
     * @return
     */
    @GetMapping("/listResource/{roleId}")
    public CommonResult listResource(@PathVariable Integer roleId){

        List<UmsResource> list = umsResourceService.listResource(roleId);

        return CommonResult.success(list);
    }

    /**
     * 给角色分配资源
     * @param roleId
     * @param resourceIds
     * @return
     */
    @PostMapping("/allocResource")
    public CommonResult allocResource(Integer roleId,@RequestParam List<Integer> resourceIds){
        umsRoleResourceRelationService.allocResource(roleId,resourceIds);
        return CommonResult.success(null);
    }
}

